/**
 * 
 */
package org.picWeb.view.background;

import static org.picWeb.util.ConfigKeys.MODEL;

import java.util.ArrayList;
import java.util.List;

import org.picWeb.entity.PicDetail;
import org.picWeb.entity.PicIndex;
import org.picWeb.util.ConfigKeys;
import org.picWeb.view.BaseAction;
import org.picWeb.view.service.PicIndexTblActionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @author zjm
 * @version org.picWeb.view.background/PicIndexTblAction.java 2011-6-22
 */
@Controller
public class PicIndexAction extends BaseAction{

	@Autowired
	private PicIndexTblActionService picIndexTblActionService;

	@RequestMapping("back/picIndexTblList")
	public String list(PicIndex picIndexTbl, Integer currentPage, String orderby, boolean desc, String _begin, String _end, ModelMap model){
		if(null == picIndexTbl){
			picIndexTbl = new PicIndex();
		}
		
		List<String> vSql = new ArrayList<String>();
		if(null!=_begin && null!=_end){
			if(null == picIndexTbl){
				picIndexTbl = new PicIndex();
			}
			vSql.add("takeTime>='" + _begin + " 00:00:00' and takeTime<='" + _end + " 23:59:59'");
		}

		this.baseActionService.list(picIndexTbl, currentPage, orderby, desc, true, vSql, model);
		
		model.addAttribute("_begin", _begin);
		model.addAttribute("_end", _end);
		
		return "background/picIndex/list";
	}

	@RequestMapping("back/picIndexTblGet")
	public String get(String id, ModelMap model) {
		String page = "background/picIndex/addMdf";
		if (null == id) {
			return page;
		}

		baseActionService.get(PicIndex.class, id, model);
		
		PicIndex index = (PicIndex) model.get(MODEL);
		String group = index.getGroupPhoto();
		String split[] = group.split("/");
		int savePathLen = ConfigKeys.paramPath.split("/").length;
		
		model.put("exitsPath", split[savePathLen]);           
		
		return page;
	}

	@RequestMapping("back/picIndexTblAddMdf")
	public String addMdf(PicIndex indexTbl, String[] originalPic, String[] middlePic, String[] smallPic) throws Exception {

		List<PicDetail> details = null;
		if (null != originalPic) {
			details = new ArrayList<PicDetail>();
			PicDetail detail ;
			for (int i = 0; i < originalPic.length; i++) {
				detail = new PicDetail();
				detail.setOriginalPic(originalPic[i]);
				detail.setMiddlePic(middlePic[i]);
				detail.setSmallPic(smallPic[i]);
				details.add(detail);
			}
		}
		
		picIndexTblActionService.addMdf(indexTbl, details);

		return "background/util/PopupSuccess";
	}
	
	@RequestMapping("back/picIndexTblDelete")
	public String delete(PicIndex picIndex, ModelMap model) throws Exception{
		picIndexTblActionService.delete(picIndex);
		return this.list(null, 0, null, false, null, null, model);
	}
}
